package dbAccess;

import java.sql.*;
import java.util.ArrayList;

import model.Section;

import exceptions.*;

public class SectionDBAccess {
	public ArrayList<Section> getAllSection() throws PasIdentificationException, DatabaseErrorException{
		ArrayList<Section> tabSect = new ArrayList<Section>();
		Connection conn = DBConnexionSingleton.getInstance();
		PreparedStatement stat;
		String query = "select * from section order by section_id;";
		try {
			stat = conn.prepareStatement(query);
			ResultSet data  = stat.executeQuery();
			while(data.next()){
				Section s = new Section(data.getInt("section_id"), data.getString("libelle"), data.getString("abrev"));
				tabSect.add(s);
			}
			stat.close();
		} catch (SQLException e) {
			throw new DatabaseErrorException(e);
		}
		return tabSect;
	}
	public Section getSection(int sectionID) throws PasIdentificationException, DatabaseErrorException{
		Section sect = null;
		Connection conn = DBConnexionSingleton.getInstance();
		PreparedStatement stat;
		String query = "select * from section where section_id = ?;";
		try{
			stat = conn.prepareStatement(query);
				stat.setInt(1, sectionID);
			ResultSet data = stat.executeQuery();
			while(data.next()){
				sect = new Section(data.getInt("section_id"), data.getString("libelle"), data.getString("abrev"));
			}
			stat.close();
		}catch (SQLException e) {
			throw new DatabaseErrorException(e);
		}
		return sect;
	}
}
